package common.com.androidbase.download;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


import com.common.sdk.net.download.DownloadManagerEx;
import com.common.sdk.net.download.callback.interfaces.IDownloadCallback;
import com.common.sdk.net.download.interfaces.IDownloadBuilder;
import com.common.sdk.net.download.request.model.DownloadGeneral;
import com.common.sdk.net.download.request.model.DownloadInfo;
import com.common.sdk.net.download.util.DownloadInfoComparatorUtils;
import com.common.sdk.net.download.util.DownloadLog;

import common.com.androidbase.R;

public class MainActivity extends Activity implements OnClickListener, IDownloadCallback {

    private IDownloadBuilder downloadRequest;
    private ListView listview_download;
    private MyAdapter adapter;
    private List<DownloadInfo> downloadInfoList;
    private volatile HashMap<String, DownloadInfo> mapInfo = new HashMap<String, DownloadInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_download);
        downloadRequest = DownloadManagerEx.getInstance();

        Button downloadButton = (Button) findViewById(R.id.download_button);
        Button downloadCancelButton = (Button) findViewById(R.id.download_cancel_button);
        Button download_recovery_button = (Button) findViewById(R.id.download_recovery_button);
        Button download_all_cancel_button = (Button) findViewById(R.id.download_all_cancel_button);
        Button download_all_recovery_button = (Button) findViewById(R.id.download_all_recovery_button);
        Button downloadButton2 = (Button) findViewById(R.id.download_button2);
        Button downloadButton3 = (Button) findViewById(R.id.download_button3);
        Button downloadButton4 = (Button) findViewById(R.id.download_button4);
        Button downloadButton5 = (Button) findViewById(R.id.download_button5);
        Button downloadButton6 = (Button) findViewById(R.id.download_button6);
        Button downloadButton7 = (Button) findViewById(R.id.download_button7);
        Button download_pause_all = (Button) findViewById(R.id.download_pause_all);
        Button download_fuifu_pause_all = (Button) findViewById(R.id.download_fuifu_pause_all);
        listview_download = (ListView) findViewById(R.id.listview_download);

        downloadButton.setOnClickListener(this);
        downloadCancelButton.setOnClickListener(this);
        download_recovery_button.setOnClickListener(this);
        download_all_cancel_button.setOnClickListener(this);
        download_all_recovery_button.setOnClickListener(this);
        downloadButton2.setOnClickListener(this);
        downloadButton3.setOnClickListener(this);
        downloadButton4.setOnClickListener(this);
        downloadButton5.setOnClickListener(this);
        downloadButton6.setOnClickListener(this);
        downloadButton7.setOnClickListener(this);
        download_pause_all.setOnClickListener(this);
        download_fuifu_pause_all.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        long time1 = System.currentTimeMillis();
        downloadRequest.registerCallback(this);
        long time2 = System.currentTimeMillis();
        DownloadLog.debug("CallbackObservableTime mainActivity " + (time2-time1));
        downloadInfoList = new ArrayList<DownloadInfo>();
        Collections.sort(downloadInfoList, new DownloadInfoComparatorUtils());
        adapter = new MyAdapter(this, downloadInfoList, downloadRequest);
        listview_download.setAdapter(adapter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        downloadRequest.unregisterCallback(this);
    }

    @Override
    public void onClick(View v) {
        // String url = "http://down.platform.app.sohu.com/?t=1&app_id=1212";
        // String url =
        // "http://y1.ifengimg.com/a/2015_26/6335633c0b7d182_size27_w417_h618.jpg";
//        String url = "http://mvvideo1.meitudata.com/557f162c3765a4920.mp4";
//        String url = "http://www.hao123.com/";
        String url = "http://video.weibo.com/show?ua=iPhone6%2C2__weibo__5.3.0__iphone__os8.4&from=1053093010&fid=1034%3A4ba696e5fc4bcaacebb13d2bad1172ca&wm=3333_2001";
        // String url =
        // "http://gdown.baidu.com/data/wisegame/f1527f906c28bba6/tianlongbabu3D_17200.apk";
        // String url =
        // "http://gdown.baidu.com/data/wisegame/df68d1bf08714e1c/baobaozhushou_220.apk";
        // String url2 =
        // "http://gdown.baidu.com/data/wisegame/341605e24d94a278/souhushipin_4640.apk";
        // String url2 =
        // "http://gdown.baidu.com/data/wisegame/e6db345fb6c3185f/didasuoping_145.apk";
        String url2 = "http://download.wan.sohu.com/distribute_platform/1/735/1.33.1.35/com.tencent.tmgp.sgame_1.33.1.35.apk";//803M
        // String url3 =
        // "http://gdown.baidu.com/data/wisegame/86ad7bc264a4e5d6/baidushoujizhushou_16785204.apk";
        String url3 = "http://download.wan.sohu.com/distribute_platform/1/725/1.0.36/com.netease.onmyoji_1.0.36.apk";//873M
        // String url4 =
        // "http://gdown.baidu.com/data/wisegame/7c7947d138644723/baiduyun_395.apk";
        String url4 = "http://download.wan.sohu.com/distribute_platform/1/734/3.0.5/com.duoyi.shenwu3_3.0.5.apk";//540M
        String url5 = "http://download.wan.sohu.com/distribute_platform/1/716/1.0/com.sszj.h5game_1.0.apk";//0.5M
        String url6 = "http://download.wan.sohu.com/distribute_platform/1/717/1.0/com.lhxc.h5game_1.0.apk";//0.6M
        String url7 = "http://download.wan.sohu.com/distribute_platform/1/722/50.3.0/game.app.m.g2.xtl_50.3.0.apk";//39M

        // /静默地址
        String url8 = "http://gdown.baidu.com/data/wisegame/a1a0330eec9b45e2/doumojianghu_5.apk";
        String url9 = "http://gdown.baidu.com/data/wisegame/796f3dd2fdbb7437/qianwangshijiedejintou_2.apk";
        String url10 = "http://gdown.baidu.com/data/wisegame/9f832a20b0c14c75/babaqunaer2_9.apk";
        String url11 = "http://bcs.apk.r1.91.com/data/upload/2015/04_13/15/com.tencent.pao_150502.apk";
        String url12 = "http://bcs.apk.r1.91.com/data/upload/2015/04_07/11/com.tencent.feiji_112501.apk";
        String url13 = "http://gdown.baidu.com/data/wisegame/71abf26641013498/leitingzhanji_100110.apk";
        String url14 = "http://gdown.baidu.com/data/wisegame/947c1a116cb1cbd9/shenmiaotaowang2_4531.apk";
        // 正常
        String url15 = "http://download.wan.sohu.com/distribute_platform/1/721/1.0.6/com.tencent.tmgp.zlong_1.0.6.apk";// 466M
        String url16 = "http://download.wan.sohu.com/distribute_platform/1/732/1.02.00/com.lumo.lxyylh.cw_1.02.00.apk";//159M
        String url17 = "http://download.wan.sohu.com/distribute_platform/1/731/1.15/com.guoziyx.platform_1.15.apk";//2.2M
        String url18 = "http://download.wan.sohu.com/distribute_platform/1/729/V1.4.31/com.sgw.hlbyr.tjby_V1.4.31.apk";//38M
        String url19 = "http://download.wan.sohu.com/distribute_platform/1/733/1.6.0.1/com.a2016158.game1_1.6.0.1.apk";//53M

        switch (v.getId()) {
            case R.id.download_button:// 添加下载
                DownloadInfo request = new DownloadGeneral(DownloadInfo.DOWNLOAD_SOURCE_OTHER,url, "mp4");

                // DownloadInfo request = new DownloadInfo(132,
                // "tianlongbabu3D", 17200, url,
                // DownloadInfo.DOWNLOAD_PRIORITY_10);
                // request.setDownloadPathTwo("dfsdfsdfsf");
                // request.setIconPicPath("IconPicPath");
                // downloadInfoList.add(request);
                // mapInfo.put(request.getKey(), request);
                /*long num = */downloadRequest.deleteAndAddDownloadItem(request);// 添加下载
                HashMap<String, DownloadInfo> map = downloadRequest.getDownloadGeneral();// 获取下载列表
                // downloadRequest.deleteDownloadItem(request);//删除任务

                // if (num == DownloadState.OPERATION_SUCCESS) {
                // // 操作成功
                // } else if (num == DownloadState.OPERATION_ALREADY_EXIST) {
                // // 已经添加过
                // } else if (num == DownloadState.OPERATION_FAILED) {
                // // 操作失败
                // } else if (num == DownloadState.OPERATION_SUCCESSED) {
                // // 已经下载成功
                // }
                break;
            case R.id.download_cancel_button:// 添加一堆静默
                DownloadInfo request8 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "doumojianghu", 5, url8, DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request8);
                DownloadInfo request9 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "qianwangshijiedejintou", 2, url9,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request9);
                DownloadInfo request10 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "babaqunaer2_", 9, url10,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request10);
                DownloadInfo request11 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "com.tencent.pao_", 150502, url11,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request11);
                DownloadInfo request12 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "com.tencent.feiji_", 11250, url12,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request12);
                DownloadInfo request13 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "leitingzhanji_", 100110, url13,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request13);
                DownloadInfo request14 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "shenmiaotaowang2_", 4531, url14,
                        DownloadInfo.DOWNLOAD_PRIORITY_5);
                downloadRequest.addDownloadItem(request14);
                break;
            case R.id.download_recovery_button:// 添加一堆正常
                DownloadInfo request15 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "doumojianghu", 5, url15,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request15);
                DownloadInfo request16 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "huochairenjujishoupojieban_", 2014041019, url16,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request16);
                DownloadInfo request17 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "tiantianxiaoxiaole", 15, url17,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request17);
                DownloadInfo request18 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "e668352289400416a087407407b", 952, url18,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request18);
                DownloadInfo request19 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "om.tencent.bigbang.", 1428003, url19,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request19);

                break;
            case R.id.download_all_cancel_button:// 取消全部
                // downloadRequest.keepNowStateStopAllTasks();
                break;
            case R.id.download_all_recovery_button:// 恢复全部
                // downloadRequest.restartAllTasks();
                // downloadRequest.recoveryNowStateAllTasks();
                break;
            case R.id.download_button2:
                DownloadInfo request118 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "menghuanxiyou_", 10140, url2,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                // downloadInfoList.add(request8);
                // mapInfo.put(request8.getKey(), request8);
                downloadRequest.addDownloadItem(request118);
                break;
            case R.id.download_button3:
                DownloadInfo request119 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "licaizhuanjia_", 150415957, url3,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                // downloadInfoList.add(request9);
                // mapInfo.put(request9.getKey(), request9);
                downloadRequest.addDownloadItem(request119);
                break;
            case R.id.download_button4:
                DownloadInfo request4 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "lieai_", 3500, url4, DownloadInfo.DOWNLOAD_PRIORITY_10);
                // downloadInfoList.add(request4);
                // mapInfo.put(request4.getKey(), request4);
                downloadRequest.addDownloadItem(request4);
                break;
            case R.id.download_button5:
                DownloadInfo request5 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "zhifubaoqianbao_", 76, url5,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                downloadRequest.addDownloadItem(request5);
                break;
            case R.id.download_button6:
                DownloadInfo request6 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "baiduditu_", 626, url6,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                // downloadInfoList.add(request6);
                // mapInfo.put(request6.getKey(), request6);
                downloadRequest.addDownloadItem(request6);
                break;
            case R.id.download_button7:
                DownloadInfo request7 = new DownloadInfo(DownloadInfo.DOWNLOAD_SOURCE_MAIN,132, "baidunuomi_", 126, url7,
                        DownloadInfo.DOWNLOAD_PRIORITY_10);
                // downloadInfoList.add(request7);
                // mapInfo.put(request7.getKey(), request7);
                downloadRequest.addDownloadItem(request7);
                break;
            case R.id.download_pause_all:
                // File imageDir = DownloadFilePathUtils.getDownloadFile();
                // File filepath = new File(imageDir,
                // DownloadFilePathUtils.getFilenameForKey(url) + ".apk");
                // filepath.delete();
                downloadRequest.pauseAllDownloadingTasks();
                break;
            case R.id.download_fuifu_pause_all:
                // File imageCreateDir =
                // DownloadFilePathUtils.getDownloadFile();
                // if (!imageCreateDir.exists()) {
                // imageCreateDir.mkdirs();
                // }
                // File fileCreatepath = new File(imageCreateDir,
                // DownloadFilePathUtils.getFilenameForKey(url) + ".apk");
                break;

            default:
                break;
        }
    }

    @Override
    public void initializationSuccess(List<DownloadInfo> downloadInfo) {
        Log.e("mainactivity", "initializationSuccess");
        if (downloadInfo != null) {
            downloadInfoList.addAll(downloadInfo);
            Collections.sort(downloadInfoList, new DownloadInfoComparatorUtils());
            for (DownloadInfo myDownloadInfo : downloadInfo) {
                mapInfo.put(myDownloadInfo.getKey(), myDownloadInfo);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void didAddDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "didAddDownloadItem");
        if (downloadInfo != null) {
            downloadInfoList.add(downloadInfo);
            Collections.sort(downloadInfoList, new DownloadInfoComparatorUtils());
            mapInfo.put(downloadInfo.getKey(), downloadInfo);
            mapInfo.get(downloadInfo.getKey()).errorCode = "已经加入下载队列";
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void waitStartDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "waitStartDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "等待开始下载";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void willStartDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "willStartDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            // mapInfo.get(downloadInfo.getKey()).errorCode = "下载即将开始开始";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void didStartDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "didStartDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载已经开始";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void willPauseDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "willPauseDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "将要暂停下载";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void didPauseDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "didPauseDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "已经暂停下载";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void willStopDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "willStopDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载将要停止";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void didStopDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "didStopDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载已经停止";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void willDeleteDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "willDeleteDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "将要删除下载";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void didDeleteDownloadItem(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "didDeleteDownloadItem");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "删除成功";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFinishedDownload(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "onFinishedDownload");
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载完成";
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onProgressDownload(DownloadInfo downloadInfo) {
        Log.e("mainactivity", "onProgressDownload" + downloadInfo.getAlreadyDownloadSize());
        if (downloadInfo != null) {
            // if(!downloadInfoList.contains(downloadInfo)){
            mapInfo.get(downloadInfo.getKey()).setSpeed(downloadInfo.getSpeed());
            mapInfo.get(downloadInfo.getKey()).setDownloadPercentage(downloadInfo.getDownloadPercentage());
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载速度：" + downloadInfo.getSpeed()/1024 + "  百分比： "
                    + downloadInfo.getDownloadPercentage() + "%";

            adapter.notifyDataSetChanged();

            // }else{
            // downloadInfoList.
            // }
        }
    }

    @Override
    public void onFailedDownload(DownloadInfo downloadInfo, int state) {
        Log.e("mainactivity", "onFailedDownload state" + state);
        // TODO Auto-generated method stub
        if (downloadInfo != null) {
            mapInfo.get(downloadInfo.getKey()).setState(downloadInfo.getState());
            mapInfo.get(downloadInfo.getKey()).errorCode = "下载失败，code：" + state;
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void noNextDownload(boolean isAllFinished) {
        Log.e("mainactivity", "noNextDownload");
        // TODO Auto-generated method stub

    }

    @Override
    public void didAddDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "didAddDownloadList");
    }

    @Override
    public void didStartDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "didStartDownloadList");
    }

    @Override
    public void didPauseDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "didPauseDownloadList");
    }

    @Override
    public void didStopDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "didStopDownloadList");
    }

    @Override
    public void didDeleteDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "didDeleteDownloadList");
    }

    @Override
    public void getNextDownloadInfo(DownloadInfo downloadInfo) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "getNextDownloadInfo");
    }

    @Override
    public void waitStartDownloadList(List<? extends DownloadInfo> infoList) {
        // TODO Auto-generated method stub
        Log.e("mainactivity", "waitStartDownloadList");
    }

}
